<?php
//检查过期的店铺账户（每天凌晨0点）
ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\brandAccount\BrandAccountAuth;
use Biz\model\brandAccount\BrandAccountInfo;
use Doraemon\cache\User as CacheUser;

//入驻过期的店铺
$today = date('Y-m-d');
$where = [
    'type' => BrandAccountInfo::ACCOUNT_TYPE_SHOP,
    'account_status' => BrandAccountInfo::ACCOUNT_STATUS_USED,
    'in_end_time[<]' => $today,
];
$accountList = BrandAccountInfo::getAllByWhere($where);
$accountList = $accountList ? array_column($accountList, null, 'uid') : [];

foreach ($accountList as $uid => $account) {
    expireShop($account['id'], $account['uid']);
    print_r('入驻过期的店铺');
    print_r([
        'account_id' => $account['id'],
        'uid' => $account['uid'],
        'in_end_time' => $account['in_end_time']
    ]);
    print_r("\n");
}

function expireShop($accountId, $uid)
{
    BrandAccountInfo::updateData([
        'account_status' => BrandAccountInfo::ACCOUNT_STATUS_EXPIRE,
        'auth_status' => BrandAccountInfo::AUTH_STATUS_EXPIRE
    ], [
        'id' => $accountId
    ]);
    BrandAccountAuth::updateData([
        'auth_status' => BrandAccountAuth::AUTH_STATUS_EXPIRE,
    ], [
        'account_id' => $accountId,
        'auth_status' => BrandAccountAuth::AUTH_STATUS_USED
    ]);

    if ($uid) {
        $userInfo = \Doraemon\model\Member::getOne($uid);
        if ($userInfo && $userInfo['type'] == \Doraemon\model\Member::AUTH_USER_BRAND) {
            //删除品牌身份
            \Doraemon\model\Member::updateUserInfo($uid, [
                'type' => \Doraemon\model\Member::USER_COMMON,
            ]);
            //强制退出登录
            \Doraemon\cache\UserAuthStatus::setAuthType($uid, \Doraemon\model\Member::USER_COMMON);
            \Doraemon\cache\EsUpdateCollector::insert($uid);
            // 删除用户缓存信息
            CacheUser::deleteUserInfo($uid);
        }
    }

}